package stack;

import static stack.ArrayStack2.*;

/**
 * @author huangxin
 * @date 2019/6/22-15:05
 */
public class Calculator {
    public static void main(String[] args) {
        String expression = "333+2*6-1+2/2";
        //数字栈
        ArrayStack2 numStack = new ArrayStack2(100);
        //符号栈
        ArrayStack2 operStack = new ArrayStack2(100);
        //用于扫描
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        //用于拼接多位数字
        String keepNum = "";
        while (true) {
            ch = expression.substring(index, index + 1).charAt(0);
            if (isOper(ch)) {
                //如果是运算符
                if (!operStack.isEmpty()) {//如果不为空
                    if (priority(ch) <= priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = cal(num1, num2, oper);
                        //把运算结果推进数字栈
                        numStack.push(res);
                        //把当前操作符推进符号栈
                        operStack.push(ch);
                    } else {
                        operStack.push(ch);
                    }
                } else {
                    //如果为空,直接入栈
                    operStack.push(ch);
                }
            } else {
                //数字,处理多位数
//                numStack.push(ch-'0');
                //拼接数字
                keepNum += ch;
                //如果ch已经是表达式的最后一位
                if (index + 1 >= expression.length()) {
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    //查看下一位是否为数字
                    if (isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        //不是数字
                        numStack.push(Integer.parseInt(keepNum));
                        //清空keepNum
                        keepNum = "";
                    }
                }
            }
            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        while (true) {
            //符号栈为空,数栈只能有一个数字,结束
            if (operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = cal(num1, num2, oper);
            numStack.push(res);
        }

        System.out.printf("表达式%s = %d\n", expression, numStack.pop());

//        for (char c : expression.toCharArray()) {
//            if (isOper(c)) {
//                if (operStack.isEmpty()) {
//                    operStack.push(c);
//                } else {
//                    int top = priority(operStack.peek());
//                    int cur = priority(c);
//                    if (cur <= top) {
//                        int num1 = numStack.pop();
//                        int num2 = numStack.pop();
//                        int oper = operStack.pop();
//                        int cal = cal(num1, num2, oper);
//                        numStack.push(cal);
//                        operStack.push(c);
//
//                    } else {
//                        operStack.push(c);
//                    }
//                }
//            } else {
//                numStack.push(c - '0');
//            }
//        }
//        int result = 0;
//        while (true) {
//            int num1 = numStack.pop();
//            int num2 = 0;
//            try {
//                num2 = numStack.pop();
//            } catch (Exception e) {
//                result = num1;
//                break;
//            }
//            int oper = operStack.pop();
//            int cal = cal(num1, num2, oper);
//            numStack.push(cal);
//        }
//        System.out.println(result);
    }
}

class ArrayStack2 {
    //栈的大小
    private int maxSize;
    //数组模拟栈
    private int[] stack;


    //top表示栈顶
    private int top = -1;

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    public boolean isFill() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int ele) {
        if (isFill()) {
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top] = ele;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        int result = stack[top];
        top--;
        return result;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈为空,没用数据");
        }
        //从栈顶开始遍历
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d] = %d\n", i, stack[i]);
        }
    }

    //返回运算符的优先级,优先级是程序员定的,数字越大,优先级越高
    public static int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    //判断是不是一个运算符号
    public static boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //计算方法
    public static int cal(int num1, int num2, int oper) {
        int result = 0;
        switch (oper) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num2 - num1;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num2 / num1;
                break;
            default:
                break;
        }
        return result;
    }

    public int peek() {
        if (top != -1) {
            return stack[top];
        }
        return 0;
    }
}
